package com.tyut.controller;

import com.tyut.pojo.Customer;
import com.tyut.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户表控制器
 */
@Controller
public class CustomerController {
    @Resource
    private CustomerService customerService;


    /**
     * 进行注册, 注册成功返回用户名
     * @return
     */
    @RequestMapping("/addCustomer")
    @ResponseBody
    public int addCustomer(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");

        String cname = request.getParameter("cname");
        String caccount = request.getParameter("caccount");
        String cpassword = request.getParameter("cpassword");
        String cgender = request.getParameter("cgender");
        String cphonenumber = request.getParameter("cphonenumber");

//        System.out.println("cname_"+cname);
//        System.out.println("caccount"+caccount);
//        System.out.println("cpassword"+cpassword);
//        System.out.println("cgender"+cgender);
//        System.out.println("cphonenumber"+cphonenumber);

        Customer customer = new Customer();
        customer.setCname(cname);
        customer.setCaccount(caccount);
        customer.setCpassword(cpassword);
        customer.setCgender(Integer.parseInt(cgender.trim()));
        customer.setCphonenumber(cphonenumber);

//        System.out.println(customer);

//        System.out.println(flag);
        if(customerService.addCustomer(customer)){
            Customer customer1 = customerService.loginCustomer(customer);
            return customer1.getCid();
        }
        return -1;
    }


    /**
     * 删除用户
     * @param cid
     */
    @RequestMapping("/deleteCustomer/{cid}")
    public void deleteCustomer(@PathVariable("cid") Integer cid,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        customerService.deleteCustomer(cid);
    }

    /**
     * 更新用户
     * @param customer
     */
    @RequestMapping("/updateCustomer")
    public void updateCustomer(Customer customer){
        customerService.updateCustomer(customer);
    }

    /**
     * 查询所有用户
     * @return
     */
    @RequestMapping("/queryListCustomer")
    @ResponseBody
    public List<Customer> queryListCustomer(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<Customer> customers = customerService.queryListCustomer();
        return customers;
    }
    /**
     * 通过用户id查询用户
     * @param cid
     * @return
     */
    @RequestMapping("/queryCustomerById/{cid}")
    @ResponseBody
    public Customer queryCustomerById(@PathVariable("cid") Integer cid,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        Customer customer = customerService.queryCustomerById(cid);
        return customer;
    }

    @RequestMapping("/loginCustomer")
    @ResponseBody
    public Customer loginCustomer(HttpServletRequest request, HttpServletResponse response){
        String caccount = request.getParameter("caccount");
        String cpassword = request.getParameter("cpassword");

//        System.out.println("caccount:"+caccount);
//        System.out.println("cpassword:"+cpassword);

        Customer customer = new Customer();
        customer.setCaccount(caccount);
        customer.setCpassword(cpassword);

        response.setHeader("Access-Control-Allow-Origin","*");

        return customerService.loginCustomer(customer);
//        System.out.println("customer1_"+customer1);

    }





}
